package com.zretc.device.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zretc.dbtools.DBTools;
import com.zretc.device.entity.Device_Need;

public class Device_NeedDao {
	/**
	 * 添加部门的需求
	 * @return
	 */
	public boolean addDevNeed(Device_Need devNeed) {
		String sql = "insert into device_need values (?,?,?,?,?,?,?,?)";
		int n=DBTools.getUpdate(sql, devNeed.getNeed_id(),devNeed.getDepartment(),devNeed.getDevice_code(),
				devNeed.getNeed_num(),devNeed.getBegin_date(),devNeed.getEnd_date(),
				devNeed.getNeed_person(),devNeed.getNeed_tele());
		
		return n>0;
		
	}
	/**
	 * 查询所有
	 * @return
	 */
    public List<Device_Need> findDevNeedAll() {
    	//创建集合
    	List<Device_Need> list = new ArrayList<Device_Need>();
    	String sql="select * from device_need";
    	ResultSet rs=DBTools.getQuery(sql, null);
    	//迭代遍历
    	try {
			while (rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				Device_Need dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
				//添加到集合
				list.add(dneed);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
    
    /**
	 * 根据设备编号模糊查询
	 * @return
	 */
    public List<Device_Need> findDevNeedByCode(String devCode) {
    	//创建集合
    	List<Device_Need> list = new ArrayList<Device_Need>();
    	String sql="select * from device_need where device_code like ?";
    	ResultSet rs=DBTools.getQuery(sql, "%"+devCode+"%");
    	//迭代遍历
    	try {
			while (rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				Device_Need dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
				//添加到集合
				list.add(dneed);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
    
    /**
	 * 根据起始日期模糊查询
	 * @return
	 */
    public List<Device_Need> findDevNeedByBegDate(String begDate) {
    	//创建集合
    	List<Device_Need> list = new ArrayList<Device_Need>();
    	String sql="select * from device_need where begin_date like ?";
    	ResultSet rs=DBTools.getQuery(sql, "%"+begDate+"%");
    	//迭代遍历
    	try {
			while (rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				Device_Need dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
				//添加到集合
				list.add(dneed);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
    
    /**
	 * 根据用户名查询
	 * @return
	 */
    public List<Device_Need> findDevNeedByUser(String needPerson) {
    	//创建集合
    	List<Device_Need> list = new ArrayList<Device_Need>();
    	String sql="select * from device_need where need_person = ?";
    	ResultSet rs=DBTools.getQuery(sql, needPerson);
    	//迭代遍历
    	try {
			while (rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				Device_Need dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
				//添加到集合
				list.add(dneed);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
    
    
    /**
	 * 根据结束日期模糊查询
	 * @return
	 */
    public List<Device_Need> findDevNeedByEndDate(String endDate) {
    	//创建集合
    	List<Device_Need> list = new ArrayList<Device_Need>();
    	String sql="select * from device_need where end_date like ?";
    	ResultSet rs=DBTools.getQuery(sql, "%"+endDate+"%");
    	//迭代遍历
    	try {
			while (rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				Device_Need dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
				//添加到集合
				list.add(dneed);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
    
    /**
     * 修改内容
     * @param dneed
     * @return 
     */
    public boolean modDevNeed(Device_Need dneed) {
    	String sql="update device_need set department=?,device_code=?,need_num=?,begin_date=?,end_date=?,need_person=?,need_tele=? where need_id=?";
    	int n=DBTools.getUpdate(sql, dneed.getDepartment(),dneed.getDevice_code(),dneed.getNeed_num(),
    			dneed.getBegin_date(),dneed.getEnd_date(),dneed.getNeed_person(),dneed.getNeed_tele(),dneed.getNeed_id());
		return n>0;
    	
	}
    /**
     * 删除需求
     * @return
     */
    public boolean delDevNeed(int dneedId) {
    	String sql="delete from device_need where need_id=?";
    	int n=DBTools.getUpdate(sql, dneedId);
		return n>0;
	}
    
    /**
	 * 根据单号查询
	 * @return
	 */
    public Device_Need findDevNeedById(int needId) {
    	//创建对象
    	Device_Need dneed = null;
    	String sql="select * from device_need where need_id=?";
    	ResultSet rs=DBTools.getQuery(sql, needId);
    	//迭代遍历
    	try {
			  if(rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dneed;
	}
    
    
    /**
	 * 根据单号查询所有
	 * @return
	 */
    public List<Device_Need> findDevNeedAllById(Device_Need devNeed) {
    	//创建集合
    	List<Device_Need> list = new ArrayList<Device_Need>();
    	String sql="select * from device_need where need_id=?";
    	ResultSet rs=DBTools.getQuery(sql, devNeed.getNeed_id());
    	//迭代遍历
    	try {
			while (rs.next()) {
				// 需求id
				int need_id=rs.getInt(1);
				// 需求部门
				String department=rs.getString(2);
				//n 设备编号
				String device_code=rs.getString(3);
				// 需求数量
				int need_num=rs.getInt(4);
				// 开始时间
				String begin_date=rs.getString(5);
				// 结束时间
				String end_date=rs.getString(6);
				// 申请人
				String need_person=rs.getString(7);
				// 申请人电话
				String need_tele=rs.getString(8);
				//赋值 
				Device_Need dneed = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date, need_person, need_tele);
				//添加到集合
				list.add(dneed);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
}
